var WxParse = require('../../wxParse/wxParse.js');
//获取应用实例
var app = getApp();
var time = 0;
var touchDot = 0;//触摸时的原点
var touchDoty = 0;//触摸时的y原点
var interval = "";
var flag_hd = true;
// 初始化数据
var init = {

  // 数据
  data: {
    content: [],
    comment: [],
    luad: false,//收藏变化
    id: '',
    num: '',
    magzine_id: '',
    periods: '',
    page_number: 1,
    nv_id: '',//类型
    pageType: '',
    nv_ids: '',
    lauds: false,//点赞头像变化
    return_lauds: '',
    pagenumber: 1,//评论页数
    shareInfo: {},//分享数据
    list_id: 'x',//点赞下标
    laudnum: false,//点赞数量
    tips: '查看所有评论',
    bar: true,//底部导航样式
    load:false,
    playvideos:1,
    back:'none',
    next:'none',

  },

  // 页面第一次加载
  onLoad: function (options) {
    var that = this;

    // 登录授权请求
    app.getLoginSession(function (session) {

      // 加载前loading效果
      that.setData({
        loading: true
      })

      // 加载后关闭loading效果
      //that.setData({
      //  loading: false
      //})
      //查看用户是否收藏接口
      wx.request({
        url: app.api,
        data: {
          app: 'magzine',
          act: 'is_collect',
          magzine_id: that.data.magzine_id,
          page_number: that.data.page_number,
          t_session: session
        },
        success: function (res) {
          var data = res.data;
          if (data.done) {
            if (data.retval == '') {
              that.setData({
                luad: false
              })
            } else {
              that.setData({
                luad: true
              })
            }
          }
          else {//失败
            that.setData({
              errorTips: app.errorTips
            })
          }
        }


      });
    });
    that.setData({
      magzine_id: options.id,
      periods: options.periods,
      page_number: options.page_number,
      nv_id: options.nv_id
    })
    if(that.data.page_number>1){
      that.setData({
        back:'block'
      })
    } else {
      that.setData({
        back: 'none'
      })
    }
    wx.request({
      url: app.api,
      data: {
        app:'magzine',
        act:'detailMagzine',
        magzine_id: that.data.magzine_id,
        periods: that.data.periods,
        page_number: that.data.page_number
      },
      // 请求成功
      success: function (res) {
        var data = res.data;

        if (data.done && data.retval) { // 成功
          var rlt = data.retval
          that.setData({
            // 关闭loading效果
            loading: false,
            content: data.retval,
            id:data.retval.id,
            page_number: data.retval.page_number,
            shareInfo: {
              title: data.retval.title,
              path: '/pages/mgzmusic/mgzmusic?id=' + that.data.magzine_id + '&periods=' + that.data.periods + '&page_number=' + that.data.page_number + '&nv_id=' + that.data.nv_id
            },

          });
          WxParse.wxParse('article', 'html', data.retval.content, that);

        } else { // 失败
          that.setData({
            errorTips: app.errorTips
          })
        }
      }
    });
    //第二个接口--推荐论文
    wx.request({
      url: app.api,
      data: {
        app:'magzine',
        act:'getVideoArticle',
        magzine_id: that.data.magzine_id,
        page_number: that.data.page_number,
        periods: that.data.periods
      },
      // 请求成功
      success: function (res) {
        var data = res.data;
        if (data.done && data.retval) { // 成功
          var rlt = data.retval
          that.setData({
            // 关闭loading效果
            loading: false,
            recomment: that.deallist(data.retval)


          });

        } else { // 失败
          that.setData({
            errorTips: app.errorTips
          })
        }
      }
    });
    //第三个接口--查看评论
    wx.request({
      url: app.api,
      data: {
        app:'magzine',
        act:'getComment',
        magzine_id: that.data.magzine_id,
        periods: that.data.periods,
        page_number: that.data.page_number,
        num: that.data.num
      },
      success: function (res) {
        var data = res.data;
        if (data.done && data.retval) {
          var rlt = data.retval
          console.log(rlt);
          that.setData({
            //关闭loading效果
            loading: false,
            comment: rlt
          })
          console.log(that.data.comment);
        }
        else {//失败
          that.setData({
            errorTips: app.errorTips
          })
        }
      },
      fail: function (res) {
        console.log("失败");
      }


    });
    //先判断下一页是什么类型，然后才能连接wx.navigatoTo
    wx.request({
      url: app.api,
      data: {
        app:'magzine',
        act:'detailMagzine',
        magzine_id: that.data.magzine_id,
        periods: that.data.periods,
        page_number: parseInt(that.data.page_number) + 1
      },
      success: function (res) {
        var data = res.data;
        var rlt = data.retval;
        if (rlt.page_number >= 1) {//判断有没有下一页
          that.setData({
            next: 'block'
          })
        }
        else {
          that.setData({
            next: 'none'
          })
        }

      },
      fail: function (res) {
        console.log("失败");
      }
    })
  },
  //用户收藏接口
  clickcollection: function (e) {
    // 登录授权
    var that = this;
    app.getLoginSession(function (session) {
      var magzine_id = that.data.content.magzine_id;
      var page_number = that.data.content.page_number;
      var periods = that.data.content.periods;
      var id = that.data.id;
      console.log(id+'id');
      if (that.data.luad == false) {//点击收藏
        wx.request({
          url: app.api,
          data: {
            app: 'magzine',
            act: 'getCollect',
            magzine_id: magzine_id,
            page_number: page_number,
            periods: periods,
            title: that.data.content.title,
            page_type: that.data.nv_id,
            t_session: session

          },
          success: function (res) {
            if (res.data.retval.status == 1) {
              that.setData({
                luad: true
              })
            } else {
              that.setData({
                luad: false
              })
              wx.showModal({
                title: '提示',
                content: '收藏失败'
              })
            }
          }

        });
      } else {//取消收藏
        wx.request({
          url: app.api,
          data: {
            app:'magzine',
            act:'dropCollect',
            id: id
          },
          success: function (res) {
            if (res.data.retval.status == 1) {
              that.setData({
                luad: false
              })
            } else {
              that.setData({
                luad: true
              })
              wx.showModal({
                title: '提示',
                content: '取消收藏失败'
              })
            }
          }

        });

      }
    })
  },
  // onShow: function () {
  //   flag_hd = true;    //重新进入页面之后，可以再次执行滑动切换页面代码
  //   clearInterval(interval); // 清除setInterval
  //   time = 0;
  // },
  // // 触摸开始事件
  // touchStart: function (e) {
  //   touchDot = e.touches[0].pageX; // 获取触摸时的原点
  //   touchDoty = e.touches[0].pageY;
  //   // 使用js计时器记录时间    
  //   interval = setInterval(function () {
  //     time++;
  //   }, 100);
  // },
  // // 触摸结束事件
  // touchEnd: function (e) {
  //   var touchMove = e.changedTouches[0].pageX;
  //   var touchMovey = e.changedTouches[0].pageY;
  //   var moveY = Math.abs(touchDoty - touchMovey);
  //   var moveX = Math.abs(touchMove - touchDot);
  //   var that = this;
  //   // 向左滑动   
  //   if (touchMove - touchDot <= -40 && time > 0 && time < 4 && flag_hd == true && moveX > moveY && moveY < 120) {
  //     flag_hd = false;
  //     //执行切换页面的方法
  //     console.log("向右滑动");
  //     that.setData({
  //       page_number: parseInt(that.data.page_number) + 1
  //     })
  //     //先判断下一页是什么类型，然后才能连接wx.navigatoTo
  //     wx.request({
  //       url: 'https://zazhi.gzgaodu.com/api/index.php?app=magzine&act=detailMagzine&magzine_id={$id}&periods={$periods}&page_number={$page_number}',
  //       data: {
  //         magzine_id: that.data.magzine_id,
  //         periods: that.data.periods,
  //         page_number: that.data.page_number
  //       },
  //       success: function (res) {
  //         var data = res.data;
  //         var rlt = data.retval;
  //         if (rlt.page_number >= 1) {//判断有没有下一页
  //           that.setData({
  //             nv_ids: rlt.page_type
  //           })
  //           if (parseInt(rlt.page_type) == 1) {
  //             that.setData({
  //               pageType: 'mgzvideo'
  //             })
  //           } else if (parseInt(rlt.page_type) == 2) {
  //             that.setData({
  //               pageType: 'mgzproduct'
  //             })
  //           } else {
  //             that.setData({
  //               pageType: 'mgzmusic'
  //             })
  //           }
  //           wx.redirectTo({//跳转到下一页
  //             url: '/pages/' + that.data.pageType + '/' + that.data.pageType + '?id=' + that.data.magzine_id + '&&periods=' + that.data.periods + '&&page_number=' + that.data.page_number + '&&nv_id=' + that.data.nv_ids
  //           })
  //         }
  //         else {
  //           wx.showModal({
  //             title: '提示',
  //             content: '没有下一页了'
  //           })
  //           that.setData({
  //             page_number: parseInt(that.data.page_number) - 1
  //           })
  //           flag_hd = true;
  //         }

  //       },
  //       fail: function (res) {
  //         console.log("失败");
  //       }
  //     })
  //   }
  //   // 向右滑动   
  //   if (touchMove - touchDot >= 40 && time > 0 && time < 4 && flag_hd == true && moveX > moveY && moveY < 120) {
  //     flag_hd = false;
  //     //执行切换页面的方法
  //     console.log("向左滑动");
  //     if (that.data.page_number > 1) {
  //       that.setData({
  //         page_number: that.data.page_number - 1
  //       })
  //       //先判断上是什么类型，然后才能连接wx.navigatoTo
  //       wx.request({
  //         url: 'https://zazhi.gzgaodu.com/api/index.php?app=magzine&act=detailMagzine&magzine_id={$id}&periods={$periods}&page_number={$page_number}',
  //         data: {
  //           magzine_id: that.data.magzine_id,
  //           periods: that.data.periods,
  //           page_number: that.data.page_number
  //         },
  //         success: function (res) {
  //           var data = res.data;
  //           var rlt = data.retval;
  //           that.setData({
  //             nv_ids: rlt.page_type
  //           })
  //           if (parseInt(rlt.page_type) == 1) {
  //             that.setData({
  //               pageType: 'mgzvideo'
  //             })
  //           } else if (parseInt(rlt.page_type) == 2) {
  //             that.setData({
  //               pageType: 'mgzproduct'
  //             })
  //           } else {
  //             that.setData({
  //               pageType: 'mgzmusic'
  //             })
  //           }
  //           wx.redirectTo({//跳转到上一页
  //             url: '/pages/' + that.data.pageType + '/' + that.data.pageType + '?id=' + that.data.magzine_id + '&&periods=' + that.data.periods + '&&page_number=' + that.data.page_number + '&&nv_id=' + that.data.nv_ids
  //           })
  //         },
  //         fail: function (res) {
  //           console.log("失败");
  //         }
  //       })
  //     } else {
  //       wx.showModal({
  //         title: '提示',
  //         content: '没有了'
  //       })
  //     }
  //   }
  //   clearInterval(interval); // 清除setInterval
  //   time = 0;
  // },
  // //触摸移动事件
  // touchmove: function (e) {
  //   var that = this;
  //   that.setData({
  //     bar: false
  //   })

  // },
  deallist: function (list) {
    if (list && Array.isArray(list)) {
      list.forEach((item) => {
        switch (parseInt(item.nv_id)) {
          case 1:
            item.page_number = 'mgzvideo'
            break
          case 2:
            item.page_number = 'mgzproduct'
            break
          case 3:
            item.page_number = 'mgzmusic'
            break
        }
      })
      return list
    }
  },
  //查看所有评论
  showall: function () {
    var that = this;
    if (that.data.tips != '显示完毕') {
      wx.request({
        url: app.api,
        data: {
          app:'magzine',
          act:'getComment',
          magzine_id: that.data.magzine_id,
          periods: that.data.periods,
          page_number: that.data.page_number,
          num: 10
        },
        success: function (res) {
          var data = res.data;
          if (data.done && data.retval) {
            var rlt = data.retval;
            if (rlt.comment != null) {
              that.setData({
                comment: rlt.comment,
                tips: '显示完毕'
              })
            }

          }
        },
        fail: function (res) {
          that.setData({
            errorTips: app.errorTips
          })
        }
      })
    }
  },
  //底部导航下一页
  next: function () {
    var that = this;
    that.setData({
      page_number: parseInt(that.data.page_number) + 1
    })
    //先判断下一页是什么类型，然后才能连接wx.navigatoTo
    wx.request({
      url: app.api,
      data: {
        app:'magzine',
        act:'detailMagzine',
        magzine_id: that.data.magzine_id,
        periods: that.data.periods,
        page_number: that.data.page_number
      },
      success: function (res) {
        var data = res.data;
        var rlt = data.retval;
        if (rlt.page_number >= 1) {//判断有没有下一页
          that.setData({
            nv_ids: rlt.page_type
          })
          if (parseInt(rlt.page_type) == 1) {
            that.setData({
              pageType: 'mgzvideo'
            })
          } else if (parseInt(rlt.page_type) == 2) {
            that.setData({
              pageType: 'mgzproduct'
            })
          } else {
            that.setData({
              pageType: 'mgzmusic'
            })
          }
          wx.redirectTo({//跳转到下一页
            url: '/pages/' + that.data.pageType + '/' + that.data.pageType + '?id=' + that.data.magzine_id + '&&periods=' + that.data.periods + '&&page_number=' + that.data.page_number + '&&nv_id=' + that.data.nv_ids
          })
        }
        else {
          wx.showModal({
            title: '提示',
            content: '没有下一页了'
          })
          that.setData({
            page_number: parseInt(that.data.page_number) - 1
          })
        }

      },
      fail: function (res) {
        console.log("失败");
      }
    })
  },
  //导航上一页
  back: function () {
    var that = this;
    if (that.data.page_number > 1) {
      that.setData({
        page_number: that.data.page_number - 1
      })
      //先判断上是什么类型，然后才能连接wx.navigatoTo
      wx.request({
        url:app.api,
        data: {
          app:'magzine',
          act:'detailMagzine',
          magzine_id: that.data.magzine_id,
          periods: that.data.periods,
          page_number: that.data.page_number
        },
        success: function (res) {
          var data = res.data;
          var rlt = data.retval;
          that.setData({
            nv_ids: rlt.page_type
          })
          if (parseInt(rlt.page_type) == 1) {
            that.setData({
              pageType: 'mgzvideo'
            })
          } else if (parseInt(rlt.page_type) == 2) {
            that.setData({
              pageType: 'mgzproduct'
            })
          } else {
            that.setData({
              pageType: 'mgzmusic'
            })
          }
          wx.redirectTo({//跳转到上一页
            url: '/pages/' + that.data.pageType + '/' + that.data.pageType + '?id=' + that.data.magzine_id + '&&periods=' + that.data.periods + '&&page_number=' + that.data.page_number + '&&nv_id=' + that.data.nv_ids
          })
        },
        fail: function (res) {
          console.log("失败");
        }
      })
    } else {
      wx.redirectTo({
        url: '/pages/mgzindex/mgzindex',
      })
    }
  },
  //点赞
  clicklaud: function (e) {
    var me = e.currentTarget.dataset.me;
    var that = this
    app.getLoginSession(function (session) {
      if (me != that.data.list_id) {//判断有没有点过赞
        wx.request({
          url: app.api,
          data: {
            app: 'magzine',
            act: 'getThumbup',
            id: e.currentTarget.dataset.id,
            t_session: session
          },
          //请求成功
          success: function (res) {
            var data = res.data;
            var rlt = data.retval;
            var me = e.currentTarget.dataset.me;//下标
            var num = e.currentTarget.dataset.num;//点赞数量
            if (data.done) {
              if (rlt.status == 1) {//点赞成功
                that.data.comment[me].thumb_up = parseInt(num) + 1;
                console.log(that.data.comment[me].thumb_up);
                console.log(parseInt(num) + 1);
                that.setData({
                  list_id: me,
                  comment: that.data.comment
                })
              } else {
                wx.showModal({
                  title: '提示',
                  content: rlt.msg
                })
              }
            }
          }
        })
      }
    })

  },
  onShareAppMessage: function () {
    return this.data.shareInfo
  },
  showimg: function () {
    var that = this;
    that.setData({
      load: true
    })
  },
  play: function () {
    this.setData({
      playvideos: 2
    })
  }

};

// 注册页面
Page(init);